package richbar.com.richbar.wxapi;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.modelpay.PayReq;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import richbar.com.richbar.R;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.help.Constants;

/**
 * Created by QiYang on 2018/12/13.
 */

public class WxUtil {
    Bitmap bitmap = null;
    public static WxUtil util = new WxUtil();
    public static WxUtil getInstance(){
        return util;
    }


    public void pay(String result){
        PayReq req = new PayReq();
        req.appId           = Constants.APP_ID;//你的微信appid
        req.partnerId       = "1900000109";//商户号
        req.prepayId        = "WX1217752501201407033233368018";//预支付交易会话ID
        req.nonceStr        = "5K8264ILTKCH16CQ2502SI8ZNMTM67VS";//随机字符串
        req.timeStamp       = "1412000000";//时间戳
        req.packageValue    = "Sign=WXPay";//扩展字段,这里固定填写Sign=WXPay
        req.sign            = "C380BEC2BFD727A4B6845133519F3AD6";//签名
        BaseApplication.iwxapi.sendReq(req);
    }


    public  void shareUrl(Context context,String url,String title,String des){
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = url;

        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title =title;
        msg.description =des;
        Bitmap thumbBmp = BitmapFactory.decodeResource(context.getResources(), R.mipmap.logo);
        msg.thumbData = bmpToByteArray(thumbBmp, true);

        //构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("webpage");
        req.message =msg;
        req.scene =SendMessageToWX.Req.WXSceneTimeline;

        BaseApplication.iwxapi.sendReq(req);
    }



    public void shareImg(final String url,final int flag){
        new Thread(new Runnable() {
            @Override
            public void run() {
                URL imageurl = null;

                try {
                    imageurl = new URL(url);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                try {
                    HttpURLConnection conn = (HttpURLConnection)imageurl.openConnection();
                    conn.setDoInput(true);
                    conn.connect();
                    InputStream is = conn.getInputStream();
                    bitmap = BitmapFactory.decodeStream(is);
                    is.close();

                    WXImageObject imgObj = new WXImageObject(bitmap);

                    WXMediaMessage msg = new WXMediaMessage();
                    msg.mediaObject = imgObj;

                    Bitmap thumbBitmap =  Bitmap.createScaledBitmap(bitmap, 100, 100, true);
                    bitmap.recycle();
                    msg.thumbData = bmpToByteArray(thumbBitmap, true);  //设置缩略图

                    SendMessageToWX.Req req = new SendMessageToWX.Req();
                    req.transaction = buildTransaction("imgshareappdata");
                    req.message = msg;
                    req.scene = flag == 1 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
                    BaseApplication.iwxapi.sendReq(req);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }




    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }



    public byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
        if (needRecycle) {
            bmp.recycle();
        }
        byte[] result = output.toByteArray();
        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


}
